﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SuperDeepZoom
{
    /// <summary>
    /// Helps with mouse events on the MSI
    /// It connects to the mouse events on the MSI and keeps track of user interaction, reporting changes
    /// to the ImageArranger
    /// </summary>
    internal class MouseMovementHelper
    {
        MultiScaleImage msi;
        ImageArranger arranger;

        bool mouseButtonPressed = false;
        bool dragInProgress = false;
        Point dragOffset = new Point();
        Point currentPosition = new Point();

        public MouseMovementHelper(SuperDeepZoom parent, ImageArranger arranger, MultiScaleImage msi)
        {
            this.msi = msi;
            this.arranger = arranger;

            msi.MouseMove += new MouseEventHandler(msi_MouseMove);
            msi.MouseLeave += new MouseEventHandler(msi_MouseLeave);
            msi.MouseLeftButtonDown += new MouseButtonEventHandler(msi_MouseLeftButtonDown);
            msi.MouseLeftButtonUp += new MouseButtonEventHandler(msi_MouseLeftButtonUp);

            //By default Silverlight has no mouse wheel support
            //This class implements it
            new MouseWheelHelper(parent).Moved += (object sender, MouseWheelEventArgs e) =>
            {
                e.Handled = true;

                if (e.Delta > 0)
                {
                    arranger.Zoom(1.2);
                }
                else
                {
                    arranger.Zoom(0.8);
                }
            };
        }

        void msi_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseButtonPressed)
            {
                //calculate new position offsets and move the MSI around that point
                dragInProgress = true;
                Point newOrigin = new Point();
                newOrigin.X = currentPosition.X - (((e.GetPosition(msi).X - dragOffset.X) / msi.ActualWidth) * msi.ViewportWidth);
                newOrigin.Y = currentPosition.Y - (((e.GetPosition(msi).Y - dragOffset.Y) / msi.ActualHeight) * msi.ViewportWidth);
                msi.ViewportOrigin = newOrigin;
            }

            //Setting the mouse location helps the arranger know where to zoom
            arranger.LastMousePosition = e.GetPosition(sender as MultiScaleImage);
        }

        private void msi_MouseLeave(object sender, MouseEventArgs e)
        {
            if (mouseButtonPressed)
            {
                mouseButtonPressed = false;
                dragInProgress = false;
            }
        }

        private void msi_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mouseButtonPressed = true;
            dragInProgress = false;
            dragOffset = e.GetPosition(msi);
            currentPosition = msi.ViewportOrigin;
        }

        private void msi_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (mouseButtonPressed && !dragInProgress)
            {
                //map to real screen point to the MSI special point
                Point p = msi.ElementToLogicalPoint(e.GetPosition(this.msi));
                
                arranger.HitTestAndZoomOnImage(p);

                bool shiftDown = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                
                arranger.LastMousePosition = e.GetPosition(msi);
                
                //Can use shift to zoom out rather than in
                if (shiftDown)
                {
                    arranger.Zoom(0.5);
                }
                else
                {
                    arranger.Zoom(2.0);
                }
            }

            mouseButtonPressed = false;
            dragInProgress = false;
        }
    }
}
